<?php
class Crypt {
        public $thiskey = '587643154876431646654873';
        
	public function encrypt($input){
		$size = mcrypt_get_block_size(MCRYPT_3DES,'ecb');
		$input = self::pkcs5_pad($input, $size);
		$key = str_pad($this->thiskey,24,'0');
		$td = mcrypt_module_open(MCRYPT_3DES, '', 'ecb', '');
		$iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
		@mcrypt_generic_init($td, $key, $iv);
		$data = mcrypt_generic($td, $input);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		//$data = base64_encode(self::PaddingPKCS7($data));
		$data = urlencode(base64_encode($data));
		return $data;
	}
	public function decrypt($encrypted){
		$encrypted = base64_decode(urldecode($encrypted));
		$key = str_pad($this->thiskey,24,'0');
		$td = mcrypt_module_open(MCRYPT_3DES,'','ecb','');
		$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND);
		$ks = mcrypt_enc_get_key_size($td);
		@mcrypt_generic_init($td, $key, $iv);
                if($encrypted==""){
                    $encrypted='1';
                }
		$decrypted = mdecrypt_generic($td, $encrypted);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		$y=self::pkcs5_unpad($decrypted);
		return $y;
	}
        public function decrypt1($encrypted){
		$encrypted = base64_decode($encrypted);
		$key = str_pad($this->thiskey,24,'0');
		$td = mcrypt_module_open(MCRYPT_3DES,'','ecb','');
		$iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td),MCRYPT_RAND);
		$ks = mcrypt_enc_get_key_size($td);
		@mcrypt_generic_init($td, $key, $iv);
                if($encrypted==""){
                    $encrypted='1';
                }
		$decrypted = mdecrypt_generic($td, $encrypted);
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		$y=self::pkcs5_unpad($decrypted);
		return $y;
	}
	public function pkcs5_pad ($text, $blocksize) {
		$pad = $blocksize - (strlen($text) % $blocksize);
		return $text . str_repeat(chr($pad), $pad);
	}
	public function pkcs5_unpad($text){
		$pad = ord($text{strlen($text)-1});
		if ($pad > strlen($text)) {
		   return false;
		}
		if (strspn($text, chr($pad), strlen($text) - $pad) != $pad){
		   return false;
		}
		return substr($text, 0, -1 * $pad);
	}
	public function PaddingPKCS7($data) {
		$block_size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
		$padding_char = $block_size - (strlen($data) % $block_size);
		$data .= str_repeat(chr($padding_char),$padding_char);
		return $data;
	}
        //3des验证串生成程序
        public function getsign($data){
            $str = array_keys($data);
            sort($str);
            
            foreach($str as $key=>$v){
                if($key==0){
                    $value = "$v=".$data[$v];
                }else{
                    $value .= "&$v=".$data[$v];
                }
            }
            return strtolower(md5($value));
            
        }
        //post请求
        public function POST_Api($url,$PostData)
	{	
		foreach ($PostData as $k => $v )
		{
			 $o .= "$k=" . urlencode ( $v ) . "&" ;
		}
		$post_data = substr ( $o , 0 ,- 1 ) ;

		//请求参数为get方式
		$curl = curl_init();
		// 设置你需要抓取的URL
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl , CURLOPT_POSTFIELDS , $post_data) ;
		// 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
		curl_setopt($curl, CURLOPT_TIMEOUT, 30);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
                curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); 
		// 运行cURL，请求网页
		$data = curl_exec($curl);
                $http_code = curl_getinfo($curl,CURLINFO_HTTP_CODE);
		// 关闭URL请求
		curl_close($curl);
                if($http_code == 200){
                    return $data;
                }elseif($http_code == 408){
                    return json_encode(array("status"=>1,"info"=>"请求超时，请稍后重试","errmsg"=>"请求超时，请稍后重试",));
                }else{
                    return json_encode(array("status"=>1,"info"=>"状态码".$http_code."：网络访问异常","errmsg"=>"状态码".$http_code."：网络访问异常"));
                }
		
	}
        //post请求
        public function GET_Api($url,$PostData)
	{	
		foreach ($PostData as $k => $v )
		{
			 $o .= "$k=" . urlencode ( $v ) . "&" ;
		}
		$post_data = substr ( $o , 0 ,- 1 ) ;
                $pos = strstr($url,"&") ? "&" : "?";
                //初始化
                $ch = curl_init();
                //设置选项，包括URL
                curl_setopt($ch, CURLOPT_URL,$url.$pos. $post_data);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                //执行并获取HTML文档内容
                $output = curl_exec($ch);
                $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
                
                //释放curl句柄
                curl_close($ch);
                //打印获得的数据
                if($http_code == 200){
                    return $output;
                }elseif($http_code == 408){
                    return json_encode(array("status"=>1,"info"=>"请求超时，请稍后重试","errmsg"=>"请求超时，请稍后重试",));
                }else{
                    return json_encode(array("status"=>1,"info"=>"状态码".$http_code."：网络访问异常","errmsg"=>"状态码".$http_code."：网络访问异常"));
                }
	}
}